package org.misalen.web.admin.exception;

import java.io.IOException;
import java.io.PrintWriter;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.log4j.Logger;
import org.misalen.web.admin.util.ServerResult;
import org.springframework.web.servlet.HandlerExceptionResolver;
import org.springframework.web.servlet.ModelAndView;

import com.fasterxml.jackson.databind.ObjectMapper;

/**
 * 异常同意处理
 * 
 * @author misaya
 *
 */
public class ExceptionHandler implements HandlerExceptionResolver {

	private final ObjectMapper mapper = new ObjectMapper();
	private static final Logger logger = Logger.getLogger(ExceptionHandler.class);

	@Override
	public ModelAndView resolveException(HttpServletRequest request, HttpServletResponse response, Object arg2,
			Exception exception) {
		response.setContentType("application/json; charset=utf-8");
		try {
			PrintWriter writer = response.getWriter();
			String json = mapper.writeValueAsString(ServerResult.error("系统异常"));
			writer.write(json);
			writer.flush();
		} catch (IOException e) {
			logger.error(e.toString());
		}
		exception.printStackTrace();
		return new ModelAndView();
	}

}
